﻿package fwk.loaderManager {	
	
	import flash.events.EventDispatcher;
	import fwk.loaderManager.events.*;
	
	/**
	* A class represent a group of LoaderItemObject to load with LoaderManager.
	*/
	public class LoaderItemGroup extends EventDispatcher
	{

		public var id:String;
		public var label:String;
		
		private var _tabItemLoader:Object;
		private var _isCompleted:Boolean;
		private var _percent:Number;
		private var _nbItemLoaded:int;
		private var _nbItem:int;
		private var _content:Object;
		
		private var _cache:Boolean = false;

		/**
		*  The content loaded. This property is define with the content loaded of the LoaderItemObject with the same id of the group if exist. Else return null.
		*  @return  The content loaded. This property is define with the content loaded of the LoaderItemObject with the same id of the group if exist. Else return null.
		*/
		public function get content():Object
		{
			return _content;
		}
		/**
		 * The array of LoaderItemObject
		 * @return The array of LoaderItemObject
		 */
		public function get tabItem():Object
		{
			return _tabItemLoader;
		}
		/**
		 * The numbers of LoaderItemObject in the group
		 * @return The numbers of items in the group
		 */
		public function get nbItem():int
		{
			return _nbItem;
		}
		/**
		 * Return the LoaderItemObject with the id parameter
		 * @param id A id of a LoaderItemObject in the group
		 * @return A LoaderItemObject if exist else return null
		 */
		public function getItem(id:String):LoaderItemObject
		{
			return _tabItemLoader[id];
		}
		/**
		*  Indicate if all LoaderItemObject in the group are completed
		*  @return true or false
		*/
		public function get completed():Boolean
		{
			return _isCompleted;
		}
		
		/**
		*  Indicate if all LoaderItemObject in the group are completed
		*  @param val true or false
		*/
		public function set completed(val:Boolean):void
		{

			if (val)
			{
				//trace("--> item group completed");
				_isCompleted = val;
				if(_tabItemLoader[id] != null) _content = (_tabItemLoader[id] as LoaderItemObject).content;
				_percent = 1;
				
				// déclenchement event
				var evt:LoaderEvent = new LoaderEvent(LoaderEvent.COMPLETED);
				evt.percent = _percent;
				this.dispatchEvent(evt);
			
			}
		}
		/**
		*  The current percent of the group loading
		*  @param val A number between 0 and 1
		*/
		public function set percent(val:Number):void
		{
			_percent = val;
			
			// déclenchement event
			var evt:LoaderEvent = new LoaderEvent(LoaderEvent.PROGRESS);
			evt.percent = _percent;
			this.dispatchEvent(evt);
		}
		/**
		*  The current percent of the group loading
		*  @return A number between 0 and 1
		*/
		public function get percent():Number
		{
			return _percent;
		}
		
		/**
		* Enable or disable the cache of the internet browser. Default false
		* @return true or false
		 */
		public function get cache():Boolean { return _cache; }
		
		/**
		* Enable or disable the cache of the internet browser. Default false
		* @param val true or false
		 */
		public function set cache(value:Boolean):void 
		{
			_cache = value;
		}
		/**
		* Create an instance of LoaderItemGroup
		* @param pid An unique id (string) for LoaderItemGroup
		* @param plabel A label string for LoaderItemGroup. Default ""
		*/
		public function LoaderItemGroup(pid:String, plabel:String = ""):void
		{
			this.id = pid;
			
			this.label = plabel;
			_tabItemLoader 	= new Object();
			_isCompleted 	= false;
			_percent 		= 0;
			_nbItem 		= 0;
			_nbItemLoaded 	= 0;
			
		}
		
		/**
		 * Add a LoaderItemObject to the group
		 * @param objToLoad A LoaderItemObject
		 */
		public function add(item:LoaderItemObject):void
		{
			_tabItemLoader[item.id] = item;
			_nbItem++;
			
			item.addEventListener(LoaderEvent.LOAD_ERROR, onErrorItem);
			item.addEventListener(LoaderEvent.PROGRESS, onProgressItem);
			item.addEventListener(LoaderEvent.COMPLETED, onCompletedItem);
		}
		
		private function onErrorItem(evt:LoaderEvent):void
		{
			var evt:LoaderEvent = new LoaderEvent(LoaderEvent.LOAD_ERROR);
			this.dispatchEvent(evt);
		}
		private function onProgressItem(evt:LoaderEvent):void
		{
			_percent = (_nbItemLoaded + evt.percent )/ _nbItem;
			
			// déclenchement event
			var evt:LoaderEvent = new LoaderEvent(LoaderEvent.PROGRESS);
			evt.percent = _percent;
			this.dispatchEvent(evt);
		}
		private function onCompletedItem(evt:LoaderEvent):void
		{
			_nbItemLoaded++;
			_percent = _nbItemLoaded / _nbItem;
			
		}
		
		/**
		 * Remove and clear internal properties of the group
		 */
		public function dispose():void
		{
			_content 		= null;
			for (var p:String in _tabItemLoader) {
				_tabItemLoader[p].dispose();
			}
			
			_isCompleted 	= false;
			_nbItemLoaded 	= 0;
			_percent 		= 0;
			
		}
	}

}
